using System;

namespace EinsteinRiddle
{
	/// <summary>
	/// Einstein's Riddle.
	/// Suppose those codes to digitalize:
	/// colourInfo: 1-White,    2-Yellow,  3-Green,  4-Red,        5-Blue.
	/// nationInfo: 1-British,  2-German,  3-Danish, 4-Swedish,    5-Norwegian.
	/// petInfo:    1-Horse,    2-Bird,    3-Dog,    4-Cat,        5-Fish.
	/// drinkInfo:  1-Water,    2-Milk,    3-Coffee, 4-Tea,        5-Beer.
	/// cigaInfo:   1-PallMall, 2-Dunhill, 3-Blend,  4-Bluemaster, 5-Prince.
	/// 
	/// Author: plusium(China)
	/// Date  : 2006-04-18
	/// </summary>
	class EinsteinRiddle
	{
		/// <summary>
		/// Main.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			int colour, nation, pet, drink, ciga;
			string colourInfo, nationInfo, petInfo, drinkInfo, cigaInfo;
			string result;
			for(drink = 1; drink < 121; drink++)
			{
				drinkInfo = getArrange(drink);
				if(drinkInfo.IndexOf("2") != 2)
				{
					continue;// not fit to 8.
				}
				for(ciga = 1; ciga < 121; ciga++)
				{
					cigaInfo = getArrange(ciga);
					if(drinkInfo.IndexOf("5") != cigaInfo.IndexOf("4"))
					{
						continue;// not fit to 12.
					}
					if(Math.Abs(cigaInfo.IndexOf("3") - drinkInfo.IndexOf("1")) !=1)
					{
						continue;// not fit to 15.
					}
					for(pet = 1; pet < 121; pet++)
					{
						petInfo = getArrange(pet);
						if(cigaInfo.IndexOf("1") != petInfo.IndexOf("2"))
						{
							continue;// not fit to 6.
						}
						if(Math.Abs(cigaInfo.IndexOf("3") - petInfo.IndexOf("4")) !=1)
						{
							continue;// not fit to 10.
						}
						if(Math.Abs(cigaInfo.IndexOf("2") - petInfo.IndexOf("1")) !=1)
						{
							continue;// not fit to 11.
						}
						for(nation = 1; nation < 121; nation++)
						{
							nationInfo = getArrange(nation);
							if(nationInfo.IndexOf("5") > 0 && nationInfo.IndexOf("5") < 4)
							{
								continue;// not fit to 9.
							}
							if(nationInfo.IndexOf("4") != petInfo.IndexOf("3"))
							{
								continue;// not fit to 2.
							}
							if(nationInfo.IndexOf("3") != drinkInfo.IndexOf("4"))
							{
								continue;// not fit to 3.
							}
							if(nationInfo.IndexOf("2") != cigaInfo.IndexOf("5"))
							{
								continue;// not fit to 13.
							}
							for(colour = 1; colour < 121; colour++)
							{
								colourInfo = getArrange(colour);
								if(colourInfo.IndexOf("3") > colourInfo.IndexOf("1"))
								{
									continue;// not fit to 4.
								}
								if(colourInfo.IndexOf("4") != nationInfo.IndexOf("1"))
								{
									continue;// not fit to 1.
								}
								if(colourInfo.IndexOf("3") != drinkInfo.IndexOf("3"))
								{
									continue;// not fit to 5.
								}
								if(colourInfo.IndexOf("2") != cigaInfo.IndexOf("2"))
								{
									continue;// not fit to 7.
								}
								if(Math.Abs(nationInfo.IndexOf("5") - colourInfo.IndexOf("5")) != 1)
								{
									continue;// not fit to 14.
								}
								result = colourInfo + "," + nationInfo + "," + petInfo + ",";
								result += drinkInfo + "," + cigaInfo;
								Console.WriteLine(result);
							}// colour
						}// nation
					}// pet
				}// ciga
			}// drink
			Console.WriteLine("Press Enter to end.");
			Console.Read();
		}

		//get one solution in a list of 120.
		private static string getArrange(int index)
		{
			string s1,s2,s3,s4,s5;
			int i1,i2,i3,i4;
			string result;
			int count=0;

			s1 = "12345";
			for(i1=0;i1<5;i1++)
			{
				s2=s1.Remove(i1,1);
				for(i2=0;i2<4;i2++)
				{
					s3=s2.Remove(i2,1);
					for(i3=0;i3<3;i3++)
					{
						s4=s3.Remove(i3,1);
						for(i4=0;i4<2;i4++)
						{
							s5=s4.Remove(i4,1);
							result = s1.Substring(i1,1) + s2.Substring(i2,1);
							result += s3.Substring(i3,1) + s4.Substring(i4,1) + s5;
							count++;
							if (count == index) return result;
						}// i4
					}// i3
				}// i2
			}// i1
			return "failed";
		}

		//can`t continue this method.
		private static string getArrange2(int index)
		{
			index--;
			string result;
			int i;
			i = 1 + (int)(index/24);
			result = i.ToString();
			return result;
		}
	}// end of class
}// end of namespace
